<?php 
// .-----------------------------------------------------------------------------------
// | Version: 2017
// |-----------------------------------------------------------------------------------
// | Author: Buddha <buddha@jipu.com>
// |-----------------------------------------------------------------------------------
// | 支付宝APP支付操作类：适用于新的接口:alipay.trade.app.pay
// .-----------------------------------------------------------------------------------

class AopClient{
	
	//应用ID
    public $appId;

    //私钥文件路径
    public $rsaPrivateKeyFilePath;

    //私钥值
    public $rsaPrivateKey;

    //网关
    public $gatewayUrl = "https://openapi.alipay.com/gateway.do";
    //返回数据格式
    public $format = "json";
    //api版本
    public $apiVersion = "1.0";

    // 表单提交字符集编码
    public $postCharset = "UTF-8";

    //使用文件读取文件格式，请只传递该值
    public $alipayPublicKey = null;

    //使用读取字符串格式，请只传递该值
    public $alipayrsaPublicKey;


    public $debugInfo = false;

    private $fileCharset = "UTF-8";

    private $RESPONSE_SUFFIX = "_response";

    private $ERROR_RESPONSE = "error_response";

    private $SIGN_NODE_NAME = "sign";


    //加密XML节点名称
    private $ENCRYPT_XML_NODE_NAME = "response_encrypted";

    private $needEncrypt = false;


    //签名类型
    public $signType = "RSA";


    //加密密钥和类型

    public $encryptKey;

    public $encryptType = "AES";

    protected $alipaySdkVersion = "alipay-sdk-php-20161101";

    /**
     * 参数组合成待签名字符串
     * @author buddha <buddha@jipu.com>
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getSignContent($params){

    	ksort($params);
    	reset($params);

    	$arg = '';
    	$i = 0;
    	foreach ($params as $key => $value) {
    		if (false === $this->checkEmpty($value) && "@" != substr($value, 0, 1)) {
          // 转换成目标字符集
          $value = $this->characet($value, $this->postCharset);
    			if($i == 0){
    				$arg .= "$key".'='."$value";
    			}else{
    				$arg .= '&'."$key".'='."$value";
    			}
    			$i++;
    		}
    	}
    	return $arg;
    }

    /**
     * RSA签名
     * @author buddha <buddha@jipu.com>
     * @param  [type] $str              待签名字符串
     * @param  [type] $private_key_path 签名商户私钥路径
     * @return [type]                   签名字符串
     */
    public function sign($str,$private_key_path){
    	if(file_exists($private_key_path)){
          $priKey = file_get_contents($private_key_path);
      }else{
          $priKey = $private_key_path;
      }
      $res = openssl_get_privatekey($priKey);
      openssl_sign($str, $sign, $res);
      openssl_free_key($res);
      //base64编码
      $sign = base64_encode($sign);
      return $sign;
    }

    /**
     * 参数数组中的value进行encode处理
     * @author buddha <buddha@jipu.com>
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getSignContentUrlencode($params){
    	ksort($params);
    	reset($params);
    	$i = 0;
    	$arg = '';
    	foreach ($params as $key => $value) {
    		if (false === $this->checkEmpty($value) && "@" != substr($value, 0, 1)) {
    			// 转换成目标字符集
          $value = $this->characet($value, $this->postCharset);
          if ($i == 0) {
          	$arg .= "$key".'='.urlencode($value);
          }else{
          	$arg .= '&'."$key".'='.urlencode($value);
          }
          $i++;
    		}
    	}
      unset($key,$value);
      //如果存在转义字符，那么去掉转义
      if (get_magic_quotes_gpc()) {
          $arg = stripslashes($arg);
      }
    	return $arg;
    }

    /**
     * 转换字符集编码
     * @author buddha <buddha@jipu.com>
     * @param  [type] $data   [待转换字符串]
     * @return [type]         [description]
     */
    public function characet($data,$Charset='UTF-8'){
    	if (!empty($data)) {
          $fileType = $this->fileCharset;
          if (strcasecmp($fileType, $Charset) != 0) {
              $data = mb_convert_encoding($data, $Charset, $fileType);
          }
      }
      return $data;
    }

    /**
     * 检验所选值是否为空
     * @author buddha <buddha@jipu.com>
     * @param  [type] $value [description]
     * @return [type]        [description]
     */
    public function checkEmpty($value){
      if (!isset($value))
        return true;
      if ($value === null)
        return true;
      if (trim($value) === "")
        return true;

      return false;
    }
}


 ?>